package com.wulinbus.controller.admin;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fancy.common.utils.StringUtil;
import com.wulinbus.model.RenderProviderModel;
import com.wulinbus.service.RenderService;
import com.wulinbus.utils.AdminAuthCheck;
@Controller
@AdminAuthCheck
@RequestMapping("/admin/render")
public class AdminRenderController {
	@Resource
	private RenderService renderService;
	
	@RequestMapping("provider_add_display.html")
	public String provider_add_display(@CookieValue(value="muid",defaultValue="") String muid,Model model,HttpServletRequest request) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{ 
			return "admin/render_provider_add";
		}
	}
	@RequestMapping("/provider_editor_display.html")
	public String provider_editor_display(@CookieValue(value="muid",defaultValue="") String muid,Integer rpid,Model model,HttpServletRequest request) {
		if(StringUtil.isEmpty(muid))
		{
			return "admin/login";
		}
		else
		{ 
			RenderProviderModel renderProviderModel=renderService.getRenderProviderModel(rpid);
			model.addAttribute("renderProviderModel", renderProviderModel);
			return "admin/render_provider_editor";
		}
	}
/**
 * 渲染提供商添加
 * @throws IOException 
 */
	@RequestMapping(value = "/provider_add.json", method = { RequestMethod.POST })
	public void provider_add(RenderProviderModel renderProviderModel,Model model,HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		renderService.insert(renderProviderModel);
		response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
	}
/**
 * 渲染提供商删除
 */
	@RequestMapping(value = "/provider_delete.json", method = { RequestMethod.POST })
	public void delete(Integer rpid,Model model,HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		if(renderService.delete(rpid)>0){
			response.getWriter().print("{\"message\":\"操作成功\",\"statusCode\":\"200\"}");
		}
		else
		{
			response.getWriter().print("{\"message\":\"操作失败\",\"statusCode\":\"300\"}");
		}
	}
/**
 * 渲染提供商编辑
 */
	@RequestMapping(value = "/provider_editor.json", method = { RequestMethod.POST })
	public void provider_editor(RenderProviderModel renderProviderModel,Integer rpid,Model model,HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json; charset=utf-8");
		renderProviderModel.setRpid(rpid);
		renderService.update(renderProviderModel);
		response.getWriter().print("{\"message\":\"操作失败\",\"statusCode\":\"300\"}");
	}
}
